package com.woniuxy.pxxt.controller;

import com.woniuxy.pxxt.common.result.Result;
import com.woniuxy.pxxt.entity.Menu;
import com.woniuxy.pxxt.entity.User;
import com.woniuxy.pxxt.service.MenuService;
import com.woniuxy.pxxt.shiro.MyJsonWebToken;
import com.woniuxy.pxxt.shiro.MyRealm;
import com.woniuxy.pxxt.utils.JWTUtil;
import com.woniuxy.pxxt.utils.TokenUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authz.Authorizer;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:
 * <P> Date: 2022/3/14:13:28
 * <P> Description:
 */
@CrossOrigin
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;




    @ApiOperation(value = "通过角色id 获得对应的权限菜单")
    @GetMapping("/findMenu")
    public Result findMenu() {
        User user = TokenUtil.getUser();
        val user1 = new User();
        user1.setId(1);
        return menuService.findMenu(user1);
    }


    @ApiOperation(value = "通过角色权限 返回对应的所有权限")
    @GetMapping("/findMenu/{roleId}")
    public Result getRoleMenu(@PathVariable Integer roleId) {

        return menuService.findMenuByRoleId(roleId);
    }

    @RequiresPermissions({"测试菜单添加"})
    @ApiOperation(value = "通过角色权限 更改所有返回值 ")
    @PutMapping ("/setMenu/{roleId}")
    public Result setMenu(@PathVariable Integer roleId,
                          @RequestBody List<Integer> menuIds) {

        return menuService.setMenu(roleId,menuIds);
    }

    @RequiresPermissions({"测试菜单添加"})
    @ApiOperation(value = "返回所有父级菜单")
    @GetMapping ("/findAllFidMenu")
    public Result findAllFidMenu() {

        return menuService.findAllFidMenu();
    }

    @RequiresPermissions({"测试菜单添加"})
    @ApiOperation(value = "增加一个子级菜单")
    @PostMapping ("/addMenuSon")
    public Result addMenuSon(@RequestBody Menu menu) {

        return menuService.addSonMenu(menu);
    }

//    @RequiresRoles({"经理"})
    @RequiresPermissions({"测试菜单添加"})
    @ApiOperation(value = "增加一个父级菜单")
    @PostMapping ("/addMenuFather")
    public Result addFatherMenu(@RequestBody Menu menu) {
//        int num = 1/0;
        return menuService.addFatherMenu(menu);
    }



}
